package bhbt.service;


import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import bhbt.model.Admin;
import bhbt.model.Member;
import bhbt.model.SysPay;
import bhbt.model.log.LogEle;
import bhbt.model.log.LogMoney;
import bhbt.service.core.BaseService;
import bhbt.service.log.LogEleService;
import bhbt.service.log.LogMoneyService;
import bhbt.utils.ContextUtil;





@Service
public class SysPayService extends BaseService {
	
	@Resource
    private MemberService memberService;
	@Resource
    private SysPayService sysPayService;
	@Resource
    private LogMoneyService logMoneyService;
	 
	@Resource
    private LogEleService logEleService;
	/**
	 * 保存
	 * @param member
	 */
	
	 
	
	public void save(SysPay sysPay,Member member) throws InterruptedException{
		//新增记录
		Admin admin = ContextUtil.getAdmin();
		sysPay.setMemberId(member.getId());
		sysPay.setAdminId(admin.getId());
		sysPayService.saveOrUpdate(sysPay);
		
		Double num = sysPay.getNum();			//现金积分
		Double num2 = sysPay.getNum2();			//保险积分
		
		System.out.println("sysPay.getType():"+sysPay.getType());
		
		if(num!=0){
			Map<String, Object> mapMember = new HashMap<String, Object>();
			mapMember.put("id",member.getId());
			mapMember.put("diyUpdate","money=money+"+num);
	        //更新余额 
	        memberService.update(mapMember);
	        
	        //重新访问数据库，获取余额
	       	member= memberService.getByKey(member.getId());
	        Double yu = member.getMoney();
	         
	        
	        //新增记录
	        LogMoney logMoney = new LogMoney();
	        logMoney.setMemberId(member.getId());
	        if(sysPay.getContent().length()>0){
	        	logMoney.setTitle("系统充值，备注："+sysPay.getContent());
	        }else {
	        	logMoney.setTitle("系统充值");
			}
	        
	        logMoney.setState(88);
	        logMoney.setTotalNum(num);
	        logMoney.setNum(num);
	        logMoney.setYu(yu);
	        logMoney.setDongNum(0d);
	        logMoney.setFaDate(new Date());
	        logMoneyService.saveOrUpdate(logMoney);
	        
		}
		
		/*if(num2!=0){
			Map<String, Object> mapMember =  new HashMap<String, Object>();
			mapMember.put("id",member.getId());
			mapMember.put("diyUpdate","ele=ele+"+num2);
	        //更新余额
	        memberService.update(mapMember);
	        
	        member= memberService.getByKey(member.getId());
	        Double yu = member.getEle();
	        
	        
	        LogEle logEle = new LogEle();
	        logEle.setMemberId(member.getId());
	        System.out.println(sysPay.getContent().length());
	        if(sysPay.getContent().length()>0){
	        	logEle.setTitle("系统充值，备注："+sysPay.getContent());
	        }else {
	        	logEle.setTitle("系统充值");
			}
	        logEle.setState(88);
	         
	        logEle.setNum(num2);
	        logEle.setYu(yu);
	        
	        logEleService.saveOrUpdate(logEle);
		}*/
  
        
    
		 
	}

}
